// 路径 /release/
let { userModel, shoopTypeModel, shoopListModel, orderListModel } = require('../model/model')
var express = require('express');
var router = express.Router();
let multiparty = require('multiparty')

/* GET users listing. */
router.get('/', function (req, res, next) {
  res.send('respond with a resource');
});
router.post('/create', (req, res) => {
  if (Array.isArray(req.body.imgSrc)) {
    req.body.imgSrc = req.body.imgSrc;
    
  }
  if (Array.isArray(req.body.label)) {
    req.body.label = req.body.label;
    
  }
  shoopListModel.create(req.body)
  res.send({
    code: 200
  })
})
router.post('/upload', (req, res) => {
  let form = new multiparty.Form({ uploadDir: 'upload' })
  form.parse(req, (err, a, b) => {
   res.send({
    url:'http://localhost:3001/'+b.source[0].path.replace(/\\/g,'/')
   })
  })
})
router.get('/gmlist', async (req, res) => {
  let { typeName, page, limit } = req.query
  let arr = []
  if (typeName) {
    arr.push({
      typeName: {
        $regex: typeName.trim()
      }
    })
  }
  let data = await shoopTypeModel.find({ $and: arr }).skip((page - 1) * limit).limit(limit)
  res.send({
    code: 200,
    data
  })


})


module.exports = router;
